package com.kaiwukj.android.ufamily.mvp.ui.page.mine.house.door;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DoorAccessDeviceResult;

import java.util.ArrayList;
import java.util.List;

public class DoorListDialog extends Dialog {
    private TextView tvTitle;
    private RecyclerView mRvList;
    private DoorAccessListAdapter mListAdapter;
    private Context mContext;
    private Button btnConfirm;
    private int type = 1;//1:社区 2:单元 3:房间

    private List<DoorAccessDeviceResult.UnitListBean> data = new ArrayList<>();

    public DoorListDialog(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getType() {
        return type;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_door_access);
        setCanceledOnTouchOutside(true);
        setCancelable(true);

        mRvList = findViewById(R.id.rv_list);
        btnConfirm = findViewById(R.id.btn_confirm);

        mListAdapter = new DoorAccessListAdapter(data, type);
        mRvList.setLayoutManager(new LinearLayoutManager(mContext));
        mRvList.setAdapter(mListAdapter);

        btnConfirm.setOnClickListener(v -> {
            if (onCheckedListener != null) {
                onCheckedListener.onCheck(mListAdapter.getChecked());
            }
        });
    }

    public void setNewData(List<DoorAccessDeviceResult.UnitListBean> datas) {
        this.data = datas;
        if (mListAdapter != null) mListAdapter.setList(datas);

    }

    public void setNewData(int type, List<DoorAccessDeviceResult.UnitListBean> datas) {
        this.type = type;
        this.data = datas;
        if (mListAdapter != null) {
            mListAdapter.setType(type);
            mListAdapter.setList(datas);
        }

    }

    public void setData(List<DoorAccessDeviceResult.UnitListBean> data) {
        this.data = data;
    }

    private OnCheckedListener onCheckedListener;

    public void setOnCheckedListener(OnCheckedListener onCheckedListener) {
        this.onCheckedListener = onCheckedListener;
    }

    public interface OnCheckedListener {
        void onCheck(DoorAccessDeviceResult.UnitListBean item);
    }
}
